package com.lzz.bottomencapsulation.view.activity.needone;


import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.lzz.bottomencapsulation.R;
import com.lzz.bottomencapsulation.mvp.MVPBaseActivity;
import com.lzz.bottomencapsulation.utils.LUtils;

/***
 * pz格式说明

 pz为zip压缩文件
 zip内压缩png序列文件及一个config文件

 config文件定义pz动画帧数、播放次数及每帧的停留时间
 pz帧顺序按照压缩进pz文件的顺序排列
 如压缩顺序为：009.png  008.png  003.png 007.png
 则第1帧为009.png 第2帧为008.png 第3帧为003.png 第4帧为007.png
 （不按照文件名顺序）


 config文件定义
 起始字节  字节数 说明
 0   4  帧数
 4   4  播放次数
 8   4  第1帧停留时间
 12   4  第2帧停留时间
 ...
 ?   4  第N帧停留时间

 每帧停留时间按照顺序排列
 */
public class NeedOneActivity extends MVPBaseActivity<NeedOneContract.View, NeedOnePresenter>
        implements NeedOneContract.View, View.OnClickListener {
    private String zipUrl = "http://ot0958jdf.bkt.clouddn.com/bottom.zip";//文件网络下载地址
    private String filePath;//文件保存至本地地址

    private Button downlode;//下载文件
    private Button readZip;//读取文件
    private Button startStop;
    private SeekBar progress;
    private AdapterViewFlipper imgShow;
    private ImageView imageView;
    private ImageView shoeAnimation;
    private Button startStopAnimation;
    private AnimationsContainer.FramesSequenceAnimation animation;
    private AnimationDrawable zipAnimationDrawable;
    private boolean start = false;
    private boolean read = false;
    private boolean isAnimation = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_needone);
        downlode = findViewById(R.id.downlode);
        readZip = findViewById(R.id.readzip);
        progress = findViewById(R.id.progress);
        imgShow = findViewById(R.id.imgshow);
        shoeAnimation = findViewById(R.id.showAnimation);
        startStopAnimation = findViewById(R.id.start_stop_Animation);
        startStop = findViewById(R.id.start_stop);
        imageView = findViewById(R.id.imageView);
        downlode.setOnClickListener(this);
        readZip.setOnClickListener(this);
        startStop.setOnClickListener(this);
        startStopAnimation.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.downlode:
//                下载pz文件
                mPresenter.downlodeZip(zipUrl);
                break;
            case R.id.readzip:
                LUtils.e(Environment.getExternalStorageDirectory().getPath());
                if (imgShow.getAdapter() == null) {
                    mPresenter.readZip();
                    read = true;
                } else {
                    if (!readBtn()) {
                        imgShow.startFlipping();
                    } else {
                        imgShow.stopFlipping();
                    }
                }

                break;
            case R.id.start_stop:
                if (animation == null) {
                    //优化后的帧动画
                    animation = AnimationsContainer.getInstance
                            ("/storage/emulated/0/DownloadFile/bottom.zip")
                            .createProgressDialogAnim(imageView);
                    animation.setOnAnimStopListener(new AnimationsContainer
                            .OnAnimationStoppedListener() {


                        @Override
                        public void AnimationStopped() {
                            LUtils.e("动画停止了");
                        }
                    });

                }
                if (!switchBtn()) {
                    animation.start();
                } else {
                    animation.stop();
                }
                break;
            case R.id.start_stop_Animation:
                if (zipAnimationDrawable == null) {
                    //优化后的帧动画
                    zipAnimationDrawable = ZipAnimationDrawable.readDrawable(mPresenter.filePath);
                    shoeAnimation.setBackground(zipAnimationDrawable);
                }
                if (!isAnimationBtn()) {
                    zipAnimationDrawable.start();
                } else {
                    zipAnimationDrawable.stop();
                }
                break;
            default:
        }
    }

    //控制开关
    private boolean switchBtn() {
        boolean returnV = start;
        start = !start;
        return returnV;
    }

    //控制开关
    private boolean readBtn() {
        boolean returnV = read;
        read = !read;
        return returnV;
    }

    //控制开关
    private boolean isAnimationBtn() {
        boolean returnV = isAnimation;
        isAnimation = !isAnimation;
        return returnV;
    }

    @Override
    public void showProgress(int currentLength) {
        progress.setProgress(currentLength);
    }

    @Override
    public void setImageAdapter(BaseAdapter adapter) {
        imgShow.setFlipInterval(2);
        imgShow.setAdapter(adapter);
        imgShow.startFlipping();
    }
}
